using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SPGE.TextureEffects
{
    public class MonoChromeEffect : CustomTextureEffect
    {
        private float val;
        public float Value
        {
            get { return val; }
            set
            {
                if (val != value && ForceCalculationOnValueChange)
                    ForceCalculate();

                val = value;
            }
        }

        public bool ForceCalculationOnValueChange
        { get; set; }

        public override bool IsVisible
        { get { return Value > 0; } }

        public Color BaseColor
        { get; set; }

        public MonoChromeEffect(float value)
            : base(EnumTextureEffectCalculation.Eager)
        {
            Value = value;
            Calculate = ToMonoChrome;
            ForceCalculationOnValueChange = false;

            BaseColor = Color.White;
        }

        private Color[] ToMonoChrome(Color[] data, int widt, int height)
        {
            Vector4 c, o;
            for (int i = 0; i < data.Length; i++)
            {
                c = data[i].ToVector4();

                if (c.W == 0)
                    continue;

                var factor = (c.X * 0.3f) + (c.Y * 0.59f) + (c.Z * 0.11f);

                o = new Vector4(BaseColor.ToVector3() * factor, c.W);

                data[i] = new Color(Vector4.Lerp(c, o, Value));
            }

            return data;
        }
    }
}
